"""
关系模型定义
定义用户、角色、权限之间的关联表
"""
from tortoise import fields, models


class UserRole(models.Model):
    """用户-角色关联表"""
    
    user = fields.ForeignKeyField("models.User", related_name="user_roles")
    role = fields.ForeignKeyField("models.Role", related_name="role_users")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    
    class Meta:
        table = "user_role"
        table_description = "用户-角色关联表"
        unique_together = ("user", "role")
        app = "models"


class UserPermission(models.Model):
    """用户-权限关联表（用户直接权限）"""
    
    user = fields.ForeignKeyField("models.User", related_name="user_permissions")
    permission = fields.ForeignKeyField("models.Permission", related_name="permission_users")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    
    class Meta:
        table = "user_permission"
        table_description = "用户-权限关联表"
        unique_together = ("user", "permission")
        app = "models"


class RolePermission(models.Model):
    """角色-权限关联表"""
    
    role = fields.ForeignKeyField("models.Role", related_name="role_permissions")
    permission = fields.ForeignKeyField("models.Permission", related_name="permission_roles")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    
    class Meta:
        table = "role_permission"
        table_description = "角色-权限关联表"
        unique_together = ("role", "permission")
        app = "models"
